package com.example.zhouqiong.richeditotandroid.db;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.Utils;
import com.example.zhouqiong.richeditotandroid.biz.constant.Constant;
import com.example.zhouqiong.richeditotandroid.biz.entity.BabyDiaryRecord;
import com.example.zhouqiong.richeditotandroid.biz.entity.BabyEntity;
import com.example.zhouqiong.richeditotandroid.biz.entity.BabyGrowthDiary;
import com.example.zhouqiong.richeditotandroid.biz.entity.BabyGrowthRecord;

import java.util.List;

public class DBUtls {

    public static void saveBaby(BabyEntity babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getDiaryEntityDao().insert(babyEntity);
    }

    public static void deleteBaby(BabyEntity babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getDiaryEntityDao().delete(babyEntity);
    }

    public static BabyEntity  getBy(int id) {
        return DBInstance.getInstance(Utils.getApp()).getDiaryEntityDao().loadBy(id);
    }

    public static List<BabyEntity> getAll() {
        return DBInstance.getInstance(Utils.getApp()).getDiaryEntityDao().getAll();
    }

    public static void saveBaby(BabyGrowthRecord babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getBabyGrowthRecordDao().insert(babyEntity);
    }

    public static void deleteBabyGrowthRecord(BabyGrowthRecord babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getBabyGrowthRecordDao().delete(babyEntity);
    }

    public static List<BabyGrowthRecord> getAllGrowth(int baby_id) {
        return DBInstance.getInstance(Utils.getApp()).getBabyGrowthRecordDao().getAll(baby_id);
    }

    public static void saveBaby(BabyDiaryRecord babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getBabyDiaryRecordDao().insert(babyEntity);
    }

    public static void deleteDiaryRecord(int id) {
        BabyDiaryRecord babyDiaryRecord = DBUtls.getDiary(id);
        if(babyDiaryRecord == null) return;
        DBInstance.getInstance(Utils.getApp()).getBabyDiaryRecordDao().delete(babyDiaryRecord);
    }

    public static void deleteDiaryRecord(BabyDiaryRecord babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getBabyDiaryRecordDao().delete(babyEntity);
    }

    public static BabyDiaryRecord getDiary(int baby_id) {
        return DBInstance.getInstance(Utils.getApp()).getBabyDiaryRecordDao().getBy(baby_id);
    }

    public static List<BabyDiaryRecord> getAllDiary(int baby_id) {
        return DBInstance.getInstance(Utils.getApp()).getBabyDiaryRecordDao().getAll(baby_id);
    }

    public static void saveBaby(BabyGrowthDiary babyEntity) {
        DBInstance.getInstance(Utils.getApp()).getBabyGrowthDiaryRecordDao().insert(babyEntity);
    }

    public static List<BabyGrowthDiary> getAllDiaryGrowth() {
        return DBInstance.getInstance(Utils.getApp()).getBabyGrowthDiaryRecordDao().getAll();
    }

    public static  BabyEntity getBabyEntity() {
        BabyEntity babyEntity = null;
        int currentId = SPUtils.getInstance().getInt(Constant.ShareKey.CURRENT_BABY_ID);
        if(currentId == -1) {
            List<BabyEntity> babyEntities = DBUtls.getAll();
            if(babyEntities.size()> 0) {
                babyEntity = babyEntities.get(0);
                SPUtils.getInstance().put(Constant.ShareKey.CURRENT_BABY_ID, babyEntity.getId());
            }
        } else {
            babyEntity = DBUtls.getBy(currentId);
        }
        return babyEntity;
    }

}
